import datetime
from typing import List

from sqlalchemy import or_, and_, func, Integer
from sqlalchemy.orm import Session

from module_admin.dao.cms_order_dispatch_dao import CmsOrderDispatchDao
from module_admin.dao.dict_dao import DictDataDao
from module_admin.entity.do.casdoor_user import CasdoorUser
from module_admin.entity.do.cms_driver import CmsDriver
from module_admin.entity.do.cms_order import CmsOrder
from module_admin.entity.do.cms_order_bill import CmsOrderBill
from module_admin.entity.do.cms_order_execute import CmsOrderExecute
from module_admin.entity.do.dict_do import SysDictData
from module_admin.entity.vo.cms_home_vo import MyTime
from module_admin.entity.vo.cms_order_vo import OrderModel, OrderModelForQuery, OrderModelForPage, \
    OrderModelForDriverPage, OrderPageObject
from utils.stringUtils import stringUtils
from utils.time_format_util import object_format_datetime, list_format_datetime
from utils.uuid import UUID


class CmsOrderDao:
    """
    部门管理模块数据库操作层
    """

    @classmethod
    def create_order(cls, db: Session, page_object: OrderModel, destination: str):
        uuid = UUID().uuid()
        page_object.order_id = "ord_" + uuid
        page_object.id = int(uuid)
        object_dict = page_object.dict()
        if "destination_list" in object_dict:
            del object_dict["destination_list"]
        if "start_time" in object_dict:
            del object_dict["start_time"]
        if "end_time" in object_dict:
            del object_dict["end_time"]
        if "use_car_type_name" in object_dict:
            del object_dict["use_car_type_name"]
        if "car_model_name" in object_dict:
            del object_dict["car_model_name"]
        if "car_type_name" in object_dict:
            del object_dict["car_type_name"]
        cms_order = CmsOrder(**object_dict)
        cms_order.destination = destination
        db.add(cms_order)
        db.flush()
        return cms_order

    @classmethod
    def delete_order_by_id(cls, db: Session, order_id: int):
        db.query(CmsOrder) \
            .filter(CmsOrder.id == order_id) \
            .delete()

    @classmethod
    def update_order_by_id(cls, db: Session, order_update_dict: dict):
        db.query(CmsOrder) \
            .filter(CmsOrder.id == order_update_dict.get('id')) \
            .update(order_update_dict)

    @classmethod
    def update_state(cls, db, id, state):
        db.query(CmsOrder) \
            .filter(CmsOrder.id == id) \
            .update({CmsOrder.state: state})
        db.commit()

    @classmethod
    def get_order_by_id(cls, db: Session, id: int):
        first = db.query(CmsOrder).filter(CmsOrder.id == id).first()
        return object_format_datetime(first)

    @classmethod
    def get_order_by_str_id(cls, db: Session, order_id: str):
        first = db.query(CmsOrder).filter(CmsOrder.order_id == order_id).first()
        return object_format_datetime(first)

    @classmethod
    def list(cls, db: Session, query_object: OrderModelForQuery):
        order_id_list = []
        if query_object.driver_id:
            order_id_list = CmsOrderDispatchDao.get_order_list_id_by_driver_id(db, query_object.driver_id)
        query = db.query(CmsOrder.id.label("id"),
                         CmsOrder.state.label("state"),
                         CmsOrder.create_time.label("create_time"),
                         CmsOrder.order_id.label("order_id"),
                         CmsOrder.user_id.label("user_id"),
                         CmsOrder.phone.label("phone"),
                         CmsOrder.department_id.label("department_id"),
                         CmsOrder.use_user_id.label("use_user_id"),
                         CmsOrder.rading_count.label("rading_count"),
                         CmsOrder.car_count.label("car_count"),
                         CmsOrder.expected_mileage.label("expected_mileage"),
                         CmsOrder.start.label("start"),
                         CmsOrder.plan_begin_time.label("plan_begin_time"),
                         CmsOrder.plan_end_time.label("plan_end_time"),
                         CmsOrder.use_car_type.label("use_car_type"),
                         CmsOrder.car_model.label("car_model"),
                         CmsOrder.car_type.label("car_type"),
                         CmsOrder.dispatch_type.label("dispatch_type"),
                         CmsOrder.remark.label("remark"),
                         CmsOrder.create_by.label("create_by"),
                         CmsOrder.update_time.label("update_time"),
                         CmsOrder.update_by.label("update_by"),
                         CmsOrder.destination.label("destination"),
                         CmsOrderExecute.bill_type.label("bill_type")
                         ) \
            .outerjoin(CmsOrderExecute, CmsOrderExecute.order_id == CmsOrder.order_id)
        filters = []
        if len(order_id_list) != 0:
            filters.append(CmsOrder.id.in_(order_id_list))
        if query_object.order_id and query_object.order_id.strip():
            filters.append(CmsOrder.order_id.like(f'%{query_object.order_id}%'))
        if query_object.user_id and query_object.user_id.strip():
            filters.append(CmsOrder.user_id.like(f'%{query_object.user_id}%'))
        if query_object.department_id and query_object.department_id.strip():
            filters.append(CmsOrder.department_id == query_object.department_id)
        if query_object.stateList is not None:
            filters.append(CmsOrder.state.in_(query_object.stateList))
        if query_object.state is not None:
            filters.append(CmsOrder.state == query_object.state)
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '用车审批':
            filters.append(CmsOrder.state.in_([1]))
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '车辆调度':
            filters.append(CmsOrder.state.in_([2]))
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '任务进行中':
            filters.append(CmsOrder.state.in_([6]))
        if query_object.start_time:
            filters.append(CmsOrder.create_time >= query_object.start_time.strftime('%Y-%m-%d %H:%M:%S'))
        if query_object.end_time:
            filters.append(CmsOrder.create_time <= query_object.end_time.strftime('%Y-%m-%d %H:%M:%S'))
        if filters:
            query = query.order_by(CmsOrder.create_time.desc()).filter(*filters)
        post_list = query.all()
        post_order_list: List[OrderModelForDriverPage] = list_format_datetime(post_list)
        post_order_list_page = []
        for e in post_order_list:
            page = OrderModelForDriverPage()
            page.id = e.id
            page.state = e.state
            page.create_time = e.create_time
            page.order_id = e.order_id
            page.user_id = e.user_id
            page.phone = e.phone
            page.department_id = e.department_id
            page.use_user_id = e.use_user_id
            page.rading_count = e.rading_count
            page.car_count = e.car_count
            page.expected_mileage = e.expected_mileage
            page.start = e.start
            page.plan_begin_time = e.plan_begin_time
            page.plan_end_time = e.plan_end_time
            page.use_car_type = e.use_car_type
            page.car_model = e.car_model
            page.car_type = e.car_type
            page.dispatch_type = e.dispatch_type
            page.remark = e.remark
            page.create_by = e.create_by
            page.update_time = e.update_time
            page.update_by = e.update_by
            page.destination_list = stringUtils.get_list(e.destination)
            page.bill_type = e.bill_type
            # 字典转换
            page_dict = page.__dict__
            DictDataDao.chang_data_value_to_code(db, "yongcheleixing", "use_car_type", page_dict["use_car_type"],
                                                 page_dict)
            DictDataDao.chang_data_value_to_code(db, "chexing", "car_model", page_dict["car_model"],
                                                 page_dict)
            DictDataDao.chang_data_value_to_code(db, "cheliangleixing", "car_type", page_dict["car_type"],
                                                 page_dict)
            page.use_car_type_name = page_dict["use_car_type_name"]
            page.car_model_name = page_dict["car_model_name"]
            page.car_type_name = page_dict["car_type_name"]
            post_order_list_page.append(page)
        return post_order_list_page

    @classmethod
    def page(cls, db, query_object: OrderPageObject):
        page_num = query_object.page_num
        page_size = query_object.page_size
        skip = (page_num - 1) * page_size  # 计算跳过的记录数
        limit = page_size  # 每页返回的记录数
        order_id_list = []
        if query_object.driver_id:
            order_id_list = CmsOrderDispatchDao.get_order_list_id_by_driver_id(db, query_object.driver_id)
        query = db.query(CmsOrder.id.label("id"),
                         CmsOrder.state.label("state"),
                         CmsOrder.create_time.label("create_time"),
                         CmsOrder.order_id.label("order_id"),
                         CmsOrder.user_id.label("user_id"),
                         CmsOrder.phone.label("phone"),
                         CmsOrder.department_id.label("department_id"),
                         CmsOrder.use_user_id.label("use_user_id"),
                         CmsOrder.rading_count.label("rading_count"),
                         CmsOrder.car_count.label("car_count"),
                         CmsOrder.expected_mileage.label("expected_mileage"),
                         CmsOrder.start.label("start"),
                         CmsOrder.plan_begin_time.label("plan_begin_time"),
                         CmsOrder.plan_end_time.label("plan_end_time"),
                         CmsOrder.use_car_type.label("use_car_type"),
                         CmsOrder.car_model.label("car_model"),
                         CmsOrder.car_type.label("car_type"),
                         CmsOrder.dispatch_type.label("dispatch_type"),
                         CmsOrder.remark.label("remark"),
                         CmsOrder.create_by.label("create_by"),
                         CmsOrder.update_time.label("update_time"),
                         CmsOrder.update_by.label("update_by"),
                         CmsOrder.destination.label("destination"),
                         CmsOrderExecute.bill_type.label("bill_type")
                         ) \
            .outerjoin(CmsOrderExecute, CmsOrderExecute.order_id == CmsOrder.order_id)
        filters = []
        if len(order_id_list) != 0:
            filters.append(CmsOrder.id.in_(order_id_list))
        if query_object.order_id and query_object.order_id.strip():
            filters.append(CmsOrder.order_id.like(f'%{query_object.order_id}%'))
        if query_object.user_id and query_object.user_id.strip():
            filters.append(CmsOrder.user_id.like(f'%{query_object.user_id}%'))
        if query_object.department_id and query_object.department_id.strip():
            filters.append(CmsOrder.department_id == query_object.department_id)
        if query_object.stateList is not None:
            filters.append(CmsOrder.state.in_(query_object.stateList))
        if query_object.state is not None:
            filters.append(CmsOrder.state == query_object.state)
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '用车审批':
            filters.append(CmsOrder.state.in_([1]))
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '车辆调度':
            filters.append(CmsOrder.state.in_([2]))
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '任务进行中':
            filters.append(CmsOrder.state.in_([6]))
        if query_object.start_time:
            filters.append(CmsOrder.create_time >= query_object.start_time.strftime('%Y-%m-%d %H:%M:%S'))
        if query_object.end_time:
            filters.append(CmsOrder.create_time <= query_object.end_time.strftime('%Y-%m-%d %H:%M:%S'))
        if filters:
            query = query.order_by(CmsOrder.create_time.desc()).filter(*filters)
        post_list = query.offset(skip).limit(limit).all()
        post_order_list: List[OrderModelForDriverPage] = list_format_datetime(post_list)
        post_order_list_page = []
        yongcheleixing_list: List[SysDictData] = DictDataDao.query_dict_data_list(db, "yongcheleixing")
        chexing_list: List[SysDictData] = DictDataDao.query_dict_data_list(db, "chexing")
        cheliangleixing_list: List[SysDictData] = DictDataDao.query_dict_data_list(db, "cheliangleixing")
        for e in post_order_list:
            page = OrderModelForDriverPage()
            page.id = e.id
            page.state = e.state
            page.create_time = e.create_time
            page.order_id = e.order_id
            page.user_id = e.user_id
            page.phone = e.phone
            page.department_id = e.department_id
            page.use_user_id = e.use_user_id
            page.rading_count = e.rading_count
            page.car_count = e.car_count
            page.expected_mileage = e.expected_mileage
            page.start = e.start
            page.plan_begin_time = e.plan_begin_time
            page.plan_end_time = e.plan_end_time
            page.use_car_type = e.use_car_type
            page.car_model = e.car_model
            page.car_type = e.car_type
            page.dispatch_type = e.dispatch_type
            page.remark = e.remark
            page.create_by = e.create_by
            page.update_time = e.update_time
            page.update_by = e.update_by
            page.destination_list = stringUtils.get_list(e.destination)
            page.bill_type = e.bill_type
            # 字典转换
            page_dict = page.__dict__
            DictDataDao.chang_data_value_to_code_new(yongcheleixing_list, "use_car_type", page_dict["use_car_type"],
                                                     page_dict)
            DictDataDao.chang_data_value_to_code_new(chexing_list, "car_model", page_dict["car_model"],
                                                     page_dict)
            DictDataDao.chang_data_value_to_code_new(cheliangleixing_list, "car_type", page_dict["car_type"],
                                                     page_dict)
            page.use_car_type_name = page_dict["use_car_type_name"]
            page.car_model_name = page_dict["car_model_name"]
            page.car_type_name = page_dict["car_type_name"]
            post_order_list_page.append(page)
        return post_order_list_page

    @classmethod
    def count(cls, db, query_object: OrderPageObject):
        order_id_list = []
        if query_object.driver_id:
            order_id_list = CmsOrderDispatchDao.get_order_list_id_by_driver_id(db, query_object.driver_id)
        query = db.query(CmsOrder.id.label("id")) \
            .outerjoin(CmsOrderExecute, CmsOrderExecute.order_id == CmsOrder.order_id)
        filters = []
        if len(order_id_list) != 0:
            filters.append(CmsOrder.id.in_(order_id_list))
        if query_object.order_id and query_object.order_id.strip():
            filters.append(CmsOrder.order_id.like(f'%{query_object.order_id}%'))
        if query_object.user_id and query_object.user_id.strip():
            filters.append(CmsOrder.user_id.like(f'%{query_object.user_id}%'))
        if query_object.department_id and query_object.department_id.strip():
            filters.append(CmsOrder.department_id == query_object.department_id)
        if query_object.stateList is not None:
            filters.append(CmsOrder.state.in_(query_object.stateList))
        if query_object.state is not None:
            filters.append(CmsOrder.state == query_object.state)
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '用车审批':
            filters.append(CmsOrder.state.in_([1]))
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '车辆调度':
            filters.append(CmsOrder.state.in_([2]))
        if query_object.state is None and query_object.card_state is not None and query_object.card_state == '任务进行中':
            filters.append(CmsOrder.state.in_([6]))
        if query_object.start_time:
            filters.append(CmsOrder.create_time >= query_object.start_time.strftime('%Y-%m-%d %H:%M:%S'))
        if query_object.end_time:
            filters.append(CmsOrder.create_time <= query_object.end_time.strftime('%Y-%m-%d %H:%M:%S'))
        if filters:
            query = query.order_by(CmsOrder.create_time.desc()).filter(*filters)
        return query.count()

    @classmethod
    def list2(cls, db, query_object: OrderModelForQuery):
        query_for_order = db.query(CmsOrder.id.label("id"),
                                   CmsOrder.order_id.label("order_id"),
                                   CmsOrder.department_id.label("department_name"),
                                   CmsOrder.dispatch_type.label("dispatch_type"),
                                   CmsOrder.use_car_type.label("use_car_type"),
                                   CmsOrder.use_user_id.label("user_name"),
                                   CmsDriver.driver_name.label("driver_name"),
                                   CmsOrder.plan_begin_time.label("plan_begin_time"),
                                   CmsOrder.plan_end_time.label("plan_end_time"),
                                   CmsOrderExecute.reality_begin_time.label("reality_begin_time"),
                                   CmsOrderExecute.reality_end_time.label("reality_end_time"),
                                   CmsOrderExecute.sheet_mileage.label("sheet_mileage"),
                                   CmsOrder.state.label("state"),
                                   CmsOrderBill.expenses.label("expenses"),
                                   CmsOrder.destination.label("destination"),
                                   CmsOrderExecute.bill_type.label("bill_type")) \
            .outerjoin(CmsOrderExecute, CmsOrderExecute.order_id == CmsOrder.order_id) \
            .outerjoin(CmsOrderBill, CmsOrderBill.order_id == CmsOrder.order_id) \
            .outerjoin(CmsDriver, CmsOrderExecute.driver_id == CmsDriver.driver_id)
        filters = []
        if query_object.order_id and query_object.order_id.strip():
            filters.append(CmsOrder.order_id == query_object.order_id)
        if query_object.user_id and query_object.user_id.strip():
            filters.append(CmsOrder.use_user_id == query_object.user_id)
        if query_object.department_id and query_object.department_id.strip():
            filters.append(CmsOrder.department_id == query_object.department_id)
        if query_object.state:
            filters.append(CmsOrder.state == query_object.state)
        if query_object.driver_id and query_object.driver_id.strip():
            filters.append(CmsOrderExecute.driver_id == query_object.driver_id)
        if query_object.bill_state and query_object.bill_state.strip():
            filters.append(CmsOrderBill.state == query_object.bill_state)
        if query_object.start_time:
            filters.append(
                CmsOrderExecute.reality_begin_time >= query_object.start_time.strftime('%Y-%m-%d %H:%M:%S'))
        if query_object.start_time:
            filters.append(
                CmsOrderExecute.reality_begin_time <= query_object.end_time.strftime('%Y-%m-%d %H:%M:%S'))
        if filters:
            query_for_order = query_for_order.filter(*filters)
        post_list_bill: List[OrderModelForPage] = list_format_datetime(
            query_for_order.order_by(CmsOrder.create_time.desc()).all())
        post_list_bill_page = []
        for e in post_list_bill:
            page = OrderModelForPage()
            page.id = e.id
            page.order_id = e.order_id
            page.department_name = e.department_name
            page.dispatch_type = e.dispatch_type
            page.use_car_type = e.use_car_type
            page.user_name = e.user_name
            page.driver_name = e.driver_name
            page.plan_begin_time = e.plan_begin_time
            page.plan_end_time = e.plan_end_time
            page.reality_begin_time = e.reality_begin_time
            page.reality_end_time = e.reality_end_time
            page.sheet_mileage = e.sheet_mileage
            page.time = (e.reality_end_time - e.reality_begin_time).seconds / 60 \
                if e.reality_end_time is not None and e.reality_begin_time is not None else None
            page.state = e.state
            page.expenses = e.expenses
            page.destination = stringUtils.get_list(e.destination)
            page.bill_type = e.bill_type
            # 字典转换
            page_dict = page.__dict__
            DictDataDao.chang_data_value_to_code(db, "yongcheleixing", "use_car_type", page_dict["use_car_type"],
                                                 page_dict)
            page.use_car_type_name = page_dict["use_car_type_name"]
            post_list_bill_page.append(page)
        return post_list_bill_page

    @classmethod
    def page2(cls, db, query_object: OrderPageObject):
        page_num = query_object.page_num
        page_size = query_object.page_size
        skip = (page_num - 1) * page_size  # 计算跳过的记录数
        limit = page_size  # 每页返回的记录数
        query_for_order = db.query(CmsOrder.id.label("id"),
                                   CmsOrder.order_id.label("order_id"),
                                   CmsOrder.department_id.label("department_name"),
                                   CmsOrder.dispatch_type.label("dispatch_type"),
                                   CmsOrder.use_car_type.label("use_car_type"),
                                   CmsOrder.use_user_id.label("user_name"),
                                   CmsDriver.driver_name.label("driver_name"),
                                   CmsOrder.plan_begin_time.label("plan_begin_time"),
                                   CmsOrder.plan_end_time.label("plan_end_time"),
                                   CmsOrderExecute.reality_begin_time.label("reality_begin_time"),
                                   CmsOrderExecute.reality_end_time.label("reality_end_time"),
                                   CmsOrderExecute.sheet_mileage.label("sheet_mileage"),
                                   CmsOrder.state.label("state"),
                                   CmsOrderBill.expenses.label("expenses"),
                                   CmsOrder.destination.label("destination"),
                                   CmsOrderExecute.bill_type.label("bill_type"),
                                   CasdoorUser.name.label("createby")
                                   ) \
            .outerjoin(CmsOrderExecute, CmsOrderExecute.order_id == CmsOrder.order_id) \
            .outerjoin(CmsOrderBill, CmsOrderBill.order_id == CmsOrder.order_id) \
            .outerjoin(CmsDriver, CmsOrderExecute.driver_id == CmsDriver.driver_id)\
            .outerjoin(CasdoorUser, CasdoorUser.name == CmsOrder.create_by)
        filters = []
        if query_object.order_id and query_object.order_id.strip():
            filters.append(CmsOrder.order_id == query_object.order_id)
        if query_object.user_id and query_object.user_id.strip():
            filters.append(CmsOrder.use_user_id == query_object.user_id)
        if query_object.department_id and query_object.department_id.strip():
            filters.append(CmsOrder.department_id == query_object.department_id)
        if query_object.state:
            filters.append(CmsOrder.state == query_object.state)
        if query_object.driver_id and query_object.driver_id.strip():
            filters.append(CmsOrderExecute.driver_id == query_object.driver_id)
        if query_object.bill_state and query_object.bill_state.strip():
            filters.append(CmsOrderBill.state == query_object.bill_state)
        if query_object.start_time:
            filters.append(
                CmsOrderExecute.reality_begin_time >= query_object.start_time.strftime('%Y-%m-%d %H:%M:%S'))
        if query_object.start_time:
            filters.append(
                CmsOrderExecute.reality_begin_time <= query_object.end_time.strftime('%Y-%m-%d %H:%M:%S'))
        if filters:
            query_for_order = query_for_order.filter(*filters)
        post_list_bill: List[OrderModelForPage] = list_format_datetime(
            query_for_order.order_by(CmsOrder.create_time.desc()).offset(skip).limit(limit).all())
        post_list_bill_page = []
        yongcheleixing_list: List[SysDictData] = DictDataDao.query_dict_data_list(db, "yongcheleixing")
        for e in post_list_bill:
            page = OrderModelForPage()
            page.id = e.id
            page.order_id = e.order_id
            page.department_name = e.department_name
            page.dispatch_type = e.dispatch_type
            page.use_car_type = e.use_car_type
            page.user_name = e.user_name
            page.driver_name = e.driver_name
            page.plan_begin_time = e.plan_begin_time
            page.plan_end_time = e.plan_end_time
            page.reality_begin_time = e.reality_begin_time
            page.reality_end_time = e.reality_end_time
            page.sheet_mileage = e.sheet_mileage
            page.createby = e.createby
            page.time = (e.reality_end_time - e.reality_begin_time).seconds / 60 \
                if e.reality_end_time is not None and e.reality_begin_time is not None else None
            page.state = e.state
            page.expenses = e.expenses
            page.destination = stringUtils.get_list(e.destination)
            page.bill_type = e.bill_type
            # 字典转换
            page_dict = page.__dict__
            DictDataDao.chang_data_value_to_code_new(yongcheleixing_list, "use_car_type", page_dict["use_car_type"],
                                                     page_dict)
            page.use_car_type_name = page_dict["use_car_type_name"]
            post_list_bill_page.append(page)
        return post_list_bill_page

    @classmethod
    def count2(cls, db, query_object: OrderPageObject):
        query_for_order = db.query(CmsOrder.id.label("id")) \
            .outerjoin(CmsOrderExecute, CmsOrderExecute.order_id == CmsOrder.order_id) \
            .outerjoin(CmsOrderBill, CmsOrderBill.order_id == CmsOrder.order_id) \
            .outerjoin(CmsDriver, CmsOrderExecute.driver_id == CmsDriver.driver_id)
        filters = []
        if query_object.order_id and query_object.order_id.strip():
            filters.append(CmsOrder.order_id == query_object.order_id)
        if query_object.user_id and query_object.user_id.strip():
            filters.append(CmsOrder.use_user_id == query_object.user_id)
        if query_object.department_id and query_object.department_id.strip():
            filters.append(CmsOrder.department_id == query_object.department_id)
        if query_object.state:
            filters.append(CmsOrder.state == query_object.state)
        if query_object.driver_id and query_object.driver_id.strip():
            filters.append(CmsOrderExecute.driver_id == query_object.driver_id)
        if query_object.bill_state and query_object.bill_state.strip():
            filters.append(CmsOrderBill.state == query_object.bill_state)
        if query_object.start_time:
            filters.append(
                CmsOrderExecute.reality_begin_time >= query_object.start_time.strftime('%Y-%m-%d %H:%M:%S'))
        if query_object.start_time:
            filters.append(
                CmsOrderExecute.reality_begin_time <= query_object.end_time.strftime('%Y-%m-%d %H:%M:%S'))
        if filters:
            query_for_order = query_for_order.filter(*filters)
        return query_for_order.order_by(CmsOrder.create_time.desc()).count()


    @classmethod
    def get_sum_state_by_time(cls, db, time: MyTime, state: str):
        mileage = db.query(func.count(CmsOrder.id)) \
            .filter(CmsOrder.state == state) \
            .filter(CmsOrder.create_time >= time.start_time.strftime('%Y-%m-%d %H:%M:%S')) \
            .filter(CmsOrder.create_time < time.end_time.strftime('%Y-%m-%d %H:%M:%S')) \
            .scalar()
        return mileage if mileage is not None else 0
